package reduce.fz.com.reduce.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.WitnessCardReplyActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.Pic;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.WitnessCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.constant.ParamConst;
import reduce.fz.com.reduce.custom.BubbleText.LeBubbleTextView;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.event.WitnessCardEvent;
import reduce.fz.com.reduce.event.WitnessEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ImageDisplay;
import reduce.fz.com.reduce.util.ScreenUtils;
import reduce.fz.com.reduce.util.VideoPlayerUtils;

/**
 * team详情我的见证动态
 */
public class TeamMeDynamicListFragment extends BaseListFragment<TeamMeDynamicListFragment.MyDynamicViewHolder, WitnessCard> {


    private Long teamid;
    private Activity mActivity;
    private Long uid;

    public static TeamMeDynamicListFragment create(Long teamid,Long uid) {
        TeamMeDynamicListFragment fragment = new TeamMeDynamicListFragment();
        fragment.setSupportLoadingDialog(false);
        Bundle bundle = new Bundle();
        bundle.putLong("teamid", teamid);
        bundle.putLong("uid",uid);
        fragment.setArguments(bundle);
        return fragment;
    }

    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<WitnessCard>>(new TypeReference<Result<List<WitnessCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<WitnessCard>> data) {
                addResult(data);
            }
        });
    }


    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();


    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, recyclerView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    public void onEvent(WitnessCardEvent event) {
        WitnessCard card = event.card;
        addFrist(card);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        teamid = getArguments().getLong("teamid");
        uid=getArguments().getLong("uid");
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    @Override
    protected MyDynamicViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_team_my_dynamic, parent, false);
        MyDynamicViewHolder viewHolder = new MyDynamicViewHolder(view, getActivity());
        return viewHolder;
    }

    @Override
    protected MyDynamicViewHolder createfootViewHolder(View view) {

        return new MyDynamicViewHolder(view, getActivity());
    }

    @Override
    protected String setUrlBeforeRequest(List<WitnessCard> datas, Pagination pagination) {
        if(uid==null) uid= UserPreference.getUser().uid;
        String url = ApiUtil.getApi(context, R.array.api_witness_dynamic, teamid, pagination.pageNo, pagination.getPageSize(),uid);
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }

    public static class MyDynamicViewHolder extends BaseRecycleViewHolder<WitnessCard> {
        private RoundNetworkImageView mUserAvatar;
        private TextView mPubTime;
        private TextView mKeepDays;
        private TextView mPubDesc;


        private GridLayout mPics;
        private TextView mPubTime2;
        private TextView mShortInfo;
        //private LinearLayout mLlShort;
        private LinearLayout mLlVideo;
        private LinearLayout mLlCard;
        private TextView mPinglun;
        private TextView mJianzheng;
        private int height = 0;
        private int margin=0;
        private WitnessCard witnessCard;
        private Activity mActivity;
        private List<String> imageList;
        private ImageView mIvShort;
        private SurfaceView mVideosurface;
        private VideoPlayerUtils playerUtils;

        /**
         * 控制显示不同版式
         *
         */
        public void display(WitnessCard card) {
            if(card==null) {
                int a=1;
                return;
            }
            switch (card.cardtype){
            //switch (teamDynamic.dynamictype) {
                /*case ParamConst.TEAM_DYNAMIC_TYPE_JOIN:
                    mLlShort.setVisibility(View.VISIBLE);
                    mLlCard.setVisibility(View.GONE);
                    renderShortDesc(teamDynamic, R.drawable.timeline_short);
                    break;
                case ParamConst.TEAM_DYNAMIC_TYPE_QUIT:
                    mLlShort.setVisibility(View.VISIBLE);
                    mLlCard.setVisibility(View.GONE);
                    renderShortDesc(teamDynamic, R.drawable.timeline_short);
                    break;*/
                case ParamConst.CARD_TYPE_PIC:
                    this.witnessCard = card;
                    imageList = initImageLit();
                    mLlVideo.setVisibility(View.GONE);
                    mLlCard.setVisibility(View.VISIBLE);
                    renderCard(card);
                    break;
                case ParamConst.CARD_TYPE_VIDEO:
                    this.witnessCard=card;
                    mLlVideo.setVisibility(View.VISIBLE);
                    mLlCard.setVisibility(View.GONE);
                    renderVideo(card);

                /*case ParamConst.TEAM_DYNAMIC_TYPE_WITNESS:
                    mLlShort.setVisibility(View.VISIBLE);
                    mLlCard.setVisibility(View.GONE);
                    renderShortDesc(teamDynamic, R.drawable.timeline_short);
                    break;
                case ParamConst.TEAM_DYNAMIC_TYPE_COMPLETE_TARGET:
                    mLlShort.setVisibility(View.VISIBLE);
                    mLlCard.setVisibility(View.GONE);
                    renderShortDesc(teamDynamic, R.drawable.timeline_short);
                    break;
                case ParamConst.TEAM_DYNAMIC_TYPE_TARGET_SET:
                    mLlShort.setVisibility(View.VISIBLE);
                    mLlCard.setVisibility(View.GONE);
                    renderShortDesc(teamDynamic, R.drawable.timeline_short);
                    break;*/
                default:
                    break;
            }

        }

        public void initWidget(View view) {


            //mIvShort = (ImageView) view.findViewById(R.id.iv_short);
            mUserAvatar = (RoundNetworkImageView) view.findViewById(R.id.iv_user_avatar);
            mPubTime = (TextView) view.findViewById(R.id.tv_pub_time);
            mKeepDays = (TextView) view.findViewById(R.id.tv_keep_days);
            mPubDesc = (TextView) view.findViewById(R.id.pub_desc);
            mPics = (GridLayout) view.findViewById(R.id.gl_pics);
            mPubTime2 = (TextView) view.findViewById(R.id.tv_pub_time2);
            mShortInfo = (TextView) view.findViewById(R.id.tv_shortinfo);
            //mLlShort = (LinearLayout) view.findViewById(R.id.ll_timeline_shortdesc);
            mLlCard = (LinearLayout) view.findViewById(R.id.ll_timeline_card);
            mLlVideo=(LinearLayout)view.findViewById(R.id.ll_timeline_video);
            mVideosurface=(SurfaceView)view.findViewById(R.id.video_surface);
            if(mVideosurface!=null) {
                playerUtils = new VideoPlayerUtils(mVideosurface);
            }
            mPinglun = (TextView) view.findViewById(R.id.tv_pinglun);
            mJianzheng = (TextView) view.findViewById(R.id.tv_jianzheng);
        }

        public MyDynamicViewHolder(View itemView, Activity mActivity) {
            super(itemView);
            this.mActivity = mActivity;
            initWidget(itemView);
            height = (ScreenUtils.getScreenW() - context.getResources().getDimensionPixelSize(R.dimen.dimen_150)
                    - context.getResources().getDimensionPixelSize(R.dimen.dimen_36)) / 3;
            margin=context.getResources().getDimensionPixelSize(R.dimen.dimen_30)/3;
        }
        public void renderVideo(final WitnessCard data)
        {



        }



        public void renderCard(final WitnessCard data) {
                mUserAvatar.setImageUrl(data.user.avatar, HttpUtils.getAvatarImageLoader());
                mPubDesc.setText(data.description);
                if(data.keepdays!=0) {
                    mKeepDays.setText("坚持第" + data.keepdays + "天");
                }
                mPinglun.setText(data.witnessAttach.reply_count + "");
                mJianzheng.setText(data.witnessAttach.witness_count + "");
                mPinglun.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        forwardComments(data);
                    }
                });
                renderPic(data);
            mPubTime.setText(DateUtil.getInterval(data.pubtime));
        }

        public void forwardComments(WitnessCard card) {
            Intent intent = new Intent(mActivity, WitnessCardReplyActivity.class);
            intent.putExtra("cardid", card.cardid);
            mActivity.startActivity(intent);
        }


        public void renderShortDesc(TeamDynamic data, int resid) {
            mIvShort.setImageResource(resid);
            mPubTime2.setText(DateUtil.getInterval(data.pubtime));
            mShortInfo.setText(data.getShortDesc());
        }

        public List<String> initImageLit() {
            List<String> imageLit = new ArrayList<String>();
            if (witnessCard.affimages == null || witnessCard.affimages.size() == 0)
                return null;
            List<Pic> pics = witnessCard.affimages;
            for (int i = 0; i < pics.size(); i++) {
                imageLit.add(pics.get(i).url);
            }
            return imageLit;
        }

        public void renderPic(WitnessCard data) {
            mPics.removeAllViews();
            for (int i = 0; i < data.affimages.size(); i++) {
                Pic pic = data.affimages.get(i);
                View view = layoutInflater.inflate(R.layout.view_timeline_pic, null);
                NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.niv_card_pic);
                RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(height,height);
                params.setMargins(0,margin,margin,0);
                imageView.setLayoutParams(params);
                imageView.setImageUrl(pic.url, HttpUtils.getImageLoader());
                final int position = i;
                mPics.addView(view);
                //  mPics.addView(view);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageDisplay.toDisplay(mActivity, (ArrayList<String>) imageList, position);
                    }
                });

            }
        }

        @Override
        public void renderView(WitnessCard data, int position) {
            display(data);


        }

    }

}
